package com.example.authority.util;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;

import java.util.ArrayList;
import java.util.List;

public class AliYun {
    public static final String apikey="sk-dcfff7f089594493a74571c64f25b7ea";
    public static String chat(String question) throws NoApiKeyException, InputRequiredException {
        List<Message> l=new ArrayList<>();
        String prompt = "你是一个AI问诊机器人，你需要回答用户关于医疗方面的问题。用户可能会把自己的体检报告给你，" +
                "并要求你输出健康建议。请根据用户的问题和体检报告，回答用户的问题。";
        Message sysMessage=Message.builder()
                .role(Role.SYSTEM.getValue())
                .content(prompt)
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(question).build();
        l.add(userMsg);
        l.add(sysMessage);
        Generation gen = new Generation();

        GenerationParam params = QwenParam.builder().model(Generation.Models.QWEN_MAX).apiKey(apikey)
                //.prompt(prompt)
                .seed(1234)
                .topP(0.8)
                .resultFormat("message")
                .enableSearch(false)
                .maxTokens(1500)
                .temperature((float)0.85)
                .repetitionPenalty((float)1.0)
                .build();

        params.setMessages(l);
        GenerationResult result = gen.call(params);

        String ans=result.getOutput().getChoices().get(0).getMessage().getContent();

        System.out.println(ans);
        return ans;
    }
}
